package com.ruoyi.circle.dao.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.circle.dao.ICircleStatisticsDao;
import com.ruoyi.circle.domain.CircleBasicInfo;
import com.ruoyi.circle.mapper.CircleBasicInfoMapper;
import com.ruoyi.circle.mapper.CircleMemberRelationMapper;
import com.ruoyi.post.domain.PostBasicInfo;
import com.ruoyi.post.mapper.PostBasicInfoMapper;

/**
 * 圈子统计数据访问层实现
 *
 * @author 区嘉盛
 * @date 2025-06-12
 */
@Repository
public class CircleStatisticsDaoImpl extends ServiceImpl<CircleBasicInfoMapper, CircleBasicInfo> implements ICircleStatisticsDao {

    @Autowired
    private CircleBasicInfoMapper circleBasicInfoMapper;

    @Autowired
    private CircleMemberRelationMapper circleMemberRelationMapper;

    @Autowired
    private PostBasicInfoMapper postBasicInfoMapper;

    /**
     * 更新圈子帖子数量
     *
     * @param circleId 圈子ID
     * @return 帖子数量
     */
    @Override
    public void updateCirclePostCount(Long circleId) {
        PostBasicInfo queryParam = new PostBasicInfo();
        queryParam.setCircleId(circleId);
        queryParam.setIsDraft(0L);
        List<PostBasicInfo> posts = postBasicInfoMapper.selectPostBasicInfoList(queryParam);
        
        // 过滤掉AI审核状态为0（待审核）和2（拒绝）的帖子，只计算状态为1（通过）的帖子
        int postCount = (int) posts.stream()
                .filter(post -> post.getAiAuditStatus() != null && post.getAiAuditStatus() == 1L)
                .count();
                
        circleBasicInfoMapper.updateCirclePostCount(circleId, postCount);
    }

    /**
     * 更新圈子热度分数
     *
     * @param circleId 圈子ID
     * @return 热度分数
     */
    @Override
    public void updateCircleHeatScore(Long circleId) {
        BigDecimal heatScore = calculateHeatScore(circleId);
        circleBasicInfoMapper.updateCircleHeatScore(circleId, heatScore);
    }

    /**
     * 计算圈子热度分数
     * 热度分数 = (成员数量 * 0.6) + (帖子数量 * 0.4)
     * 注意：只计算AI审核状态为通过的帖子
     *
     * @param circleId 圈子ID
     * @return 热度分数
     */
    @Override
    public BigDecimal calculateHeatScore(Long circleId) {
        int memberCount = circleMemberRelationMapper.selectCircleMemberCount(circleId);
        PostBasicInfo queryParam = new PostBasicInfo();
        queryParam.setCircleId(circleId);
        queryParam.setIsDraft(0L);
        List<PostBasicInfo> posts = postBasicInfoMapper.selectPostBasicInfoList(queryParam);
        
        // 过滤掉AI审核状态为0（待审核）和2（拒绝）的帖子，只计算状态为1（通过）的帖子
        int postCount = (int) posts.stream()
                .filter(post -> post.getAiAuditStatus() != null && post.getAiAuditStatus() == 1L)
                .count();
                
        BigDecimal memberScore = BigDecimal.valueOf(memberCount).multiply(BigDecimal.valueOf(0.6));
        BigDecimal postScore = BigDecimal.valueOf(postCount).multiply(BigDecimal.valueOf(0.4));

        return memberScore.add(postScore).setScale(2, RoundingMode.HALF_UP);
    }
}